package com.bussiness.system.service;

import com.bussiness.system.dao.MyAuthDao;
import com.bussiness.system.dao.MyRoleAuthDao;
import com.bussiness.system.dao.MyRoleDao;
import com.bussiness.system.model.MyAuthEntity;
import com.bussiness.system.model.MyRoleAuthEntity;
import com.bussiness.system.model.MyRoleEntity;
import com.bussiness.system.vo.MyRoleVo;
import com.bussiness.util.CommonUtils;
import com.bussiness.system.dto.MyRoleAuthDto;
import com.common.util.TreeVoUtil;
import com.common.util.mypage.Pages;
import com.common.util.pagehelper.PageResult;
import com.common.util.pagehelper.PageUtils;
import com.common.vo.TreeVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @description:
 * @author: zyj
 * @create: 2020-01-06 17:25
 **/
@Service
public class RoleAuthService {

    @Autowired
    private TreeVoUtil treeVoUtil;

    @Autowired
    private MyRoleDao myRoleDao;

    @Autowired
    private MyRoleAuthDao myRoleAuthDao;

    @Autowired
    private MyAuthDao myAuthDao;

    public TreeVo getTree(List<MyAuthEntity> myAuthEntities){
        return treeVoUtil.getTreeVo(getTreeVo(myAuthEntities), "0");
    }

    public List<TreeVo> getTreeVo(List<MyAuthEntity> cloudAuthEntitys){
        List<TreeVo> list = new ArrayList<>();
        for(int i=0;i<cloudAuthEntitys.size();i++){
            MyAuthEntity cloudAuthEntity = cloudAuthEntitys.get(i);
            TreeVo treeVo = new TreeVo();
            treeVo.setId(String.valueOf(cloudAuthEntity.getId()));
            treeVo.setIcon(cloudAuthEntity.getIcon());
            treeVo.setName(cloudAuthEntity.getName());
            treeVo.setParentId(String.valueOf(cloudAuthEntity.getPid()));
            treeVo.setUrl(cloudAuthEntity.getUrl());
            treeVo.setValue(cloudAuthEntity.getUrl());

            list.add(treeVo);
        }

        return list;
    }

    /**
    * @Description: 角色列表
    * @Param: [pageSize, pageNo]
    * @return: com.github.pagehelper.Page
    * @Author: zyj
    * @Date: 2020/1/7
    */
    public PageResult rolePage(Integer pageSize, Integer pageNo) throws Exception{
        PageHelper.startPage(pageNo, pageSize);
        List<MyRoleEntity> list =  myRoleDao.selectList();
        return PageUtils.getPageResult(new PageInfo<>(list));
    }

    public Pages roleMyPage(Integer pageSize, Integer pageNo,String name) throws Exception{
        Map<String, Object> paramMap = new HashMap<>();
        Pages pages = new Pages(pageNo, pageSize);
        paramMap.put("roleName", name);
        pages.setParams(paramMap);
        List<MyRoleEntity> list = myRoleDao.pageList(pages);
        MyRoleVo myRoleVo = new MyRoleVo();
        List<MyRoleVo> lists = myRoleVo.coverList(list);
        pages.setRows(lists);
        return pages;
    }

    /**
     * 添加修改角色资源 先删除再添加
     * @author zyj
     * @date 2019/9/24 9:34
     * @return
     */
    @Transactional(value="transactionManager", rollbackFor = Exception.class)
    public void updateRoleAuth(List<MyRoleAuthDto> myRoleAuthDtos) throws Exception{
        List<MyRoleAuthEntity> myRoleAuthEntities = new ArrayList<>();
        for(int i=0;i<myRoleAuthDtos.size();i++){
            MyRoleAuthEntity cloudRoleAuthEntity = new MyRoleAuthEntity(myRoleAuthDtos.get(i));
            cloudRoleAuthEntity.setRoleAuthStatus("1");
            myRoleAuthEntities.add(cloudRoleAuthEntity);
        }

        //根据角色id删除
        myRoleAuthDao.deleteByPrimaryRoleId(Long.parseLong(myRoleAuthDtos.get(0).getRoleId()));
        //添加角色资源
        myRoleAuthDao.insertListSelective(myRoleAuthEntities);
    }

    /**
     * 数据
     * @return
     */
    public List<TreeVo> getChildrenTree(String id){

//        List<MyAuthEntity> resourceList = myAuthDao.getAuthByParentId(null);
//        listTrees = treeVoUtil.convertTreeVo(CommonUtils.coverTree(resourceList), "1");
        List<TreeVo> listTrees = null;
        List<MyAuthEntity> resourceList = null ;
        List<MyAuthEntity> childrenRe = new ArrayList<>();
        try {

            listTrees = new ArrayList<TreeVo>();
            resourceList = myAuthDao.getAuthByParentId(null);
            Iterator<MyAuthEntity> iterator = resourceList.iterator();
            while (iterator.hasNext()){//查找id对应子节点
                MyAuthEntity resource = iterator.next();
                Long parentId = resource.getPid();
                long localId = resource.getId();
                if(parentId!=null&&parentId.equals(Long.parseLong(id))&&localId!=parentId){
                    childrenRe.add(resource);
                }
            }

            //转TreeVo
            listTrees = coverTree(childrenRe, resourceList);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return  listTrees;
    }

    /**
     * 转换成树结构
     * @param resourceList 需要转换的list
     * @param resourceList1 所有资源
     * @return
     */
    public List<TreeVo> coverTree(List<MyAuthEntity> resourceList,List<MyAuthEntity> resourceList1){
        List<TreeVo> listTree = new ArrayList<>();
        if(resourceList!=null&&resourceList.size()>0){
            Iterator<MyAuthEntity> iterator = resourceList.iterator();
            while (iterator.hasNext()){
                MyAuthEntity next = iterator.next();
                long id = next.getId();
                TreeVo treeVo = new TreeVo();
                treeVo.setId(String.valueOf(id));
                treeVo.setName(next.getName()+"-"+id);
                treeVo.setParentId(String.valueOf(next.getPid()));
                //判断是否为父级
                boolean b = false;
                Iterator<MyAuthEntity> iterator1 = resourceList1.iterator();
                while (iterator1.hasNext()){
                    MyAuthEntity next1 = iterator1.next();
                    if (next1.getPid()==id){
                        b = true;
                    }
                }

                treeVo.setIsParent(b);
                listTree.add(treeVo);
            }
        }

        return  listTree;
    }

    /**
     * 获取资源树
     * @return
     */
    public List<TreeVo> getResource(Long roleId){
        List<TreeVo> listTrees = null;
        try {
            List<MyAuthEntity> resourceList = myAuthDao.getAuthByParentId(null);
            List<MyRoleAuthEntity> roleAuthEntities = myRoleAuthDao.selectByRoleId(roleId);
            listTrees = treeVoUtil.convertTreeVo(CommonUtils.coverTree(resourceList, roleAuthEntities), "0");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return listTrees;
    }


}
